import * as THREE from 'three';
import { SimObject, City } from '..';
import { Power, RoadAccess } from './modules';
import { loadTexture } from '../../utils';

const statusIcons = {
    'no-power': loadTexture(`./statusIcons/no-power.png`, true),
    'no-road-access': loadTexture(`./statusIcons/no-road-access.png`, true)
};

export class Building extends SimObject {

    static status = {
        NoPower: 'no-power',
        NoRoadAccess: 'no-road-access',
        Ok: 'ok'
    };

    static type = {
        residential: 'residential',
        commercial: 'commercial',
        industrial: 'industrial',
        road: 'road',
        powerPlant: 'power-plant',
        powerLine: 'power-line'
    };

    /**
     * The building type
     */
    type = 'building';
    /**
     * True if the terrain should not be rendered with this building type
     */
    hideTerrain = false;

    power = new Power();

    roadAccess = new RoadAccess(this);

    status = Building.status.Ok;

    private statusIcon = new THREE.Sprite();


    constructor() {
        super();

        this.statusIcon.visible = false;
        this.statusIcon.material = new THREE.SpriteMaterial({ depthTest: false });
        this.statusIcon.layers.set(1);
        this.statusIcon.scale.set(0.5, 0.5, 0.5);
        this.add(this.statusIcon);
    }

    //todo 怪怪的，后续再优化
    setStatus(status: string) {

        if (status !== this.status) {
            switch (status) {
                case Building.status.NoPower:
                    this.statusIcon.visible = true;
                    this.statusIcon.material.map = statusIcons[Building.status.NoPower];
                    break;
                case Building.status.NoRoadAccess:
                    this.statusIcon.visible = true;
                    this.statusIcon.material.map = statusIcons[Building.status.NoRoadAccess];
                    break;
                default:
                    this.statusIcon.visible = false;
            }
        }
    }

    simulate(city: City) {
        super.simulate(city);

        this.power.simulate(city);
        this.roadAccess.simulate(city);

        if (!this.power.isFull) {
            this.setStatus(Building.status.NoPower);
        } else if (!this.roadAccess.value) {
            this.setStatus(Building.status.NoRoadAccess);
        } else {
            this.setStatus(null);
        }

    }

    refreshView(city) {

    }

    dispose() {

    }

    toHTML() {

        let html = `
        <div class="info-heading">Building</div>
        <span class="info-label">Name </span>
        <span class="info-value">${this.name}</span>
        <br>
        <span class="info-label">Type </span>
        <span class="info-value">${this.type}</span>
        <br>
        <span class="info-label">Road Access </span>
        <span class="info-value">${this.roadAccess.value}</span>
        <br>`;

        if (this.power.required > 0) {
            html += `
          <span class="info-label">Power (kW)</span>
          <span class="info-value">${this.power.supplied}/${this.power.required}</span>
          <br>`;
        }
        return html;

    }
}